﻿using Newtonsoft.Json;

namespace System
{
    /// <summary>
    /// 数据转换
    /// </summary>
    public static class ConvertData
    {
        /// <summary>
        /// 安全获取值，当值为null时，不会抛出异常
        /// </summary>
        /// <param name="value">可空值</param>
        public static T SafeValue<T>(this T? value) where T : struct
        {
            return value ?? default(T);
        }
        /// <summary>
        /// 转换为JSON字符串
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static string ToJSON(this object obj)
        {
            JsonSerializerSettings setting = new JsonSerializerSettings();

            //日期类型默认格式化处理
            setting.DateFormatHandling = DateFormatHandling.MicrosoftDateFormat;
            setting.DateFormatString = "yyyy-MM-dd HH:mm:ss";
            setting.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
            ////NULL值处理
            //setting.NullValueHandling = NullValueHandling.Ignore;

            return JsonConvert.SerializeObject(obj, Formatting.Indented, setting);
        }
        /// <summary>
        /// JSON字符串转实体
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="str"></param>
        /// <returns></returns>
        public static T ToEntity<T>(this string str)
        {
            return JsonConvert.DeserializeObject<T>(str);
        }
        /// <summary>
        /// 验证字符串是否是日期格式
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool IsDateTime(this string str)
        {
            try
            {
                Convert.ToDateTime(str);
                return true;
            }
            catch
            {
                return false;
            }
        }
        /// <summary>
        /// 转换为字符串数据
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static string ToStr(this object obj)
        {
            if (obj == null || obj == DBNull.Value || string.IsNullOrWhiteSpace(obj.ToString()))
            {
                return string.Empty;
            }
            return obj.ToString().Trim();
        }

        /// <summary>
        /// 转换为整数数据(默认0)
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static int ToInt(this object obj)
        {
            return obj.ToInt(0);
        }
        /// <summary>
        /// 转换为整数数据
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="def">默认值</param>
        /// <returns></returns>
        public static int ToInt(this object obj, int def)
        {
            if (obj == null || obj == DBNull.Value)
            {
                return def;
            }
            try
            {
                return Convert.ToInt32(obj);
            }
            catch
            {
                return def;
            }
        }
        /// <summary>
        /// 转换为二进制数据(默认0)
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static byte ToByte(this object obj)
        {
            return obj.ToByte(0);
        }
        /// <summary>
        /// 转换为二进制数据
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="def">默认值</param>
        /// <returns></returns>
        public static byte ToByte(this object obj, byte def)
        {
            if (obj == null || obj == DBNull.Value)
            {
                return def;
            }
            try
            {
                return Convert.ToByte(obj);
            }
            catch
            {
                return def;
            }
        }
        /// <summary>
        /// 转换为二进制数组数据
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static byte[] ToByteArray(this object obj)
        {
            return obj.ToByteArray(new byte[1] { 0 });
        }
        /// <summary>
        /// 转换为二进制数组数据
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="def">默认值</param>
        /// <returns></returns>
        public static byte[] ToByteArray(this object obj, byte[] def)
        {
            if (obj == null || obj == DBNull.Value)
            {
                return def;
            }
            try
            {
                return (byte[])obj;
            }
            catch
            {
                return def;
            }
        }
        /// <summary>
        /// 转换为数字数据(默认0)
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static decimal ToDecimal(this object obj)
        {
            return obj.ToDecimal(0);
        }
        /// <summary>
        /// 转换为数字数据
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="def">默认值</param>
        /// <returns></returns>
        public static decimal ToDecimal(this object obj, decimal def)
        {
            if (obj == null || obj == DBNull.Value)
            {
                return def;
            }
            try
            {
                return Convert.ToDecimal(obj);
            }
            catch
            {
                return def;
            }
        }
        /// <summary>
        /// 转换为布尔数据(默认false)
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static bool ToBoolean(this object obj)
        {
            return obj.ToBoolean(false);
        }
        /// <summary>
        /// 转换为布尔数据
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="def">默认值</param>
        /// <returns></returns>
        public static bool ToBoolean(this object obj, bool def)
        {
            if (obj == null || obj == DBNull.Value)
            {
                return def;
            }
            try
            {
                return Convert.ToBoolean(obj);
            }
            catch
            {
                return def;
            }
        }
        /// <summary>
        /// 转换为时间数据(默认MinValue)
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static DateTime ToDateTime(this object obj)
        {
            return obj.ToDateTime(DateTime.MinValue);
        }
        /// <summary>
        /// 转换为时间数据
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="def">默认值</param>
        /// <returns></returns>
        public static DateTime ToDateTime(this object obj, DateTime def)
        {
            if (obj == null || obj == DBNull.Value)
            {
                return def;
            }
            try
            {
                return Convert.ToDateTime(obj);
            }
            catch
            {
                return def;
            }
        }
    }
}
